package com.example.redstarcommerce.cn.dh.history.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.redstarcommerce.cn.dh.common.vo.Result;
import com.example.redstarcommerce.cn.dh.history.mapper.HistoryMapper;
import com.example.redstarcommerce.cn.dh.history.pojo.History;
import com.example.redstarcommerce.cn.dh.history.service.HistoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.List;

/**
 * @ClassName HistoryServiceImpl
 * @Author AndyWu
 * @Date 2024-11-01 22:43
 * @Motto 学不死就往死里学
 * @Version 1.0
 */
@Service
@Slf4j
public class HistoryServiceImpl implements HistoryService {

    @Autowired
    private HistoryMapper historyMapper;

    @Override
    public Result<Boolean> insertHistory(String uid, String name, String type, String introduce, Integer scoernumber, Timestamp begintime, Timestamp endtime, String map) {
        History history = new History(uid, name, type, introduce, scoernumber, begintime, endtime, map);
        int insert = historyMapper.insert(history);
        if (insert <= 0){
            return Result.fail("插入失败");
        }
        return Result.success(true);
    }

    @Override
    public Result<List<History>> getAllHistory() {
        QueryWrapper queryWrapper = new QueryWrapper();
        List<History> list = historyMapper.selectList(queryWrapper);
        if (list.isEmpty()){
            return Result.fail("查询失败");
        }
        return Result.success(list);
    }

    @Override
    public Result<Boolean> updateHistory(String uid, String name, String type, String introduce, Integer scoernumber, Timestamp begintime, Timestamp endtime) {
        History history = new History(uid, name, type, introduce, scoernumber, begintime, endtime);
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("uid",uid);
        int update = historyMapper.update(history, queryWrapper);
        if (update <= 0){
            return Result.fail("修改失败");
        }
        return Result.success(true);
    }

    @Override
    public Result<Boolean> deleteHistory(String uid) {

        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("uid",uid);
        int delete = historyMapper.delete(queryWrapper);
        if (delete <= 0){
            return Result.fail("删除失败");
        }
        return Result.success(true);
    }
}
